package com.jiuyi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuyi.common.R;
import com.jiuyi.entity.Employee;
import com.jiuyi.exception.SystemException;
import com.jiuyi.mapper.EmployeeMapper;
import com.jiuyi.service.EmployeeService;
import com.jiuyi.utils.BeanCopyUtils;
import com.jiuyi.vo.AdminSelectEmployeeByIdVo;
import com.jiuyi.vo.AdminSelectEmployeeVo;
import com.jiuyi.vo.PageVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.jiuyi.emum.HttpMsgMenu.*;

/**
 * DATA: 2022/10/17
 * Author: 晨夕
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 登录
     */
    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {
        //1.判断账号是否存在
        LambdaQueryWrapper<Employee> query = new LambdaQueryWrapper<>();
        query.eq(Employee::getUsername,employee.getUsername());
        Employee employeeIsExist = employeeMapper.selectOne(query);
        if(ObjectUtils.isEmpty(employeeIsExist)){
            throw new SystemException(USERNAME_NOT_EXIST);
        }
        //2.判断账号是否为空
        String username = employee.getUsername();
        if(ObjectUtils.isEmpty(username)){
            throw new SystemException(NO_USERNAME);
        }
        //3.判断密码是否为空
        String password = employee.getPassword();
        if(ObjectUtils.isEmpty(password)){
            throw new SystemException(NO_PASSWORD);
        }
        //4.对密码进行md5加密
        String newPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        //5.将加密后的密码和数据库中的密码进行比对，根据用户名
        String exitPassword = employeeMapper.selectPasswordByUsername(employee.getUsername());
        if(!newPassword.equals(exitPassword)){
            throw new SystemException(PASSWORD_ERROR);
        }
        //6.查看用户状态：禁用 or 不禁用
        if(employeeIsExist.getStatus() == 0){
            throw new SystemException(ACCOUNT_BANNED);
        }
        //7.登录成功，将员工id存入Session中，并返回成功登录结果
        request.getSession().setAttribute("employee",employeeIsExist.getId());
        return R.success(employee);
    }
    /**
     * 登出
     */
    @Override
    public R<Employee> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工
     */
    @Override
    public R addEmployee(HttpServletRequest request ,Employee employee) {
        //1. 账号、身份证、手机号、性别、姓名不能为空
        notNull(employee);
        //2. 判断用户名、身份证、手机号是否存在
        //判断用户名是否存在
        LambdaQueryWrapper<Employee> query = new LambdaQueryWrapper<>();
        query.eq(Employee::getUsername,employee.getUsername());
        Employee employeeUsername = employeeMapper.selectOne(query);
        if(!ObjectUtils.isEmpty(employeeUsername)){
            throw new SystemException(USERNAME_IS_EXIST);
        }
        //判断身份证是否存在
        query.eq(Employee::getIdNumber,employee.getIdNumber());
        Employee employeeIdNumber = employeeMapper.selectOne(query);
        if(!ObjectUtils.isEmpty(employeeIdNumber)){
            throw new SystemException(IDNUMBER_IS_EXIST);
        }
        //判断手机号是否存在
        query.eq(Employee::getPhone,employee.getPhone());
        Employee employeePhone = employeeMapper.selectOne(query);
        if(!ObjectUtils.isEmpty(employeePhone)){
            throw new SystemException(PHONE_IS_EXIST);
        }
        //3. 设置默认初识密码
        String password = "123456";
        String defaultPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(defaultPassword);
        //4. 插入数据
        employeeMapper.insert(employee);
        return R.success("添加成功");
    }

    /**
     * 查询员工
     */
    @Override
    public R<AdminSelectEmployeeVo> selectEmployee(int page, int pageSize, String name) {
        //1. 根据姓名查询，并根据修改时间降序排序
        List<Employee> employeeList = employeeMapper.selectEmployeeList((page - 1) * pageSize,pageSize,name);
        int count = employeeMapper.selectEmployeeCount(name);
        List<AdminSelectEmployeeVo> adminSelectEmployeeVos = BeanCopyUtils.copyBeanList(employeeList, AdminSelectEmployeeVo.class);
        //2. 分页
        PageVo<AdminSelectEmployeeVo> pageVo = new PageVo<>();
        pageVo.setRecords(adminSelectEmployeeVos);
        pageVo.setTotal(count);
        return R.ok(pageVo);
    }

    /**
     * 修改员工
     */
    @Override
    public R updateEmployee(HttpServletRequest request, Employee employee) {
        //1. 判断用户名、身份证、手机号是否存在
        int usernameCount = employeeMapper.selectUsernameIsExist(employee.getUsername(),employee.getId());
        if(usernameCount > 0){
            throw new SystemException(USERNAME_IS_EXIST);
        }
        int idNumberCount = employeeMapper.selectIdNumberIsExist(employee.getIdNumber(),employee.getId());
        if(idNumberCount > 0){
            throw new SystemException(IDNUMBER_IS_EXIST);
        }
        int phoneCount = employeeMapper.selectPhoneIsExist(employee.getPhone(),employee.getId());
        if(phoneCount > 0){
            throw new SystemException(PHONE_IS_EXIST);
        }
        employeeMapper.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id查询
     */
    @Override
    public R<AdminSelectEmployeeByIdVo> selectEmployeeById(Long id) {
        Employee employee = employeeMapper.selectById(id);
        AdminSelectEmployeeByIdVo adminSelectEmployeeByIdVo = BeanCopyUtils.copyBean(employee, AdminSelectEmployeeByIdVo.class);
        if(ObjectUtils.isEmpty(adminSelectEmployeeByIdVo)){
            throw new SystemException(USERNAME_NOT_EXIST);
        }
        return R.ok(adminSelectEmployeeByIdVo);
    }

    /**
     * 不能为空的数据
     */
    public void notNull(Employee employee){
        if(ObjectUtils.isEmpty(employee.getUsername())){
            throw new SystemException(NO_USERNAME);
        }
        if(ObjectUtils.isEmpty(employee.getUsername())){
            throw new SystemException(NO_NAME);
        }
        if(ObjectUtils.isEmpty(employee.getUsername())){
            throw new SystemException(NO_PHONE);
        }
        if(ObjectUtils.isEmpty(employee.getUsername())){
            throw new SystemException(NO_SEX);
        }
        if(ObjectUtils.isEmpty(employee.getUsername())){
            throw new SystemException(NO_IDNUMBER);
        }
    }
}
